import functions from "./functions.js";
import {
	getToken
} from "@/utils/auth.js";
const apiUrl = `${functions.getDomain()}/api/port/upload`;

export default function(params) {
	return new Promise((relove, reject) => {
		uni.chooseImage({
			count: params.count, //默认9
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], //从相册选择
			success: function(res) {
				// let path = res.tempFilePaths[0];
				console.log("res", res);
				uni.showLoading({
					title: '上传中',
					mask: true
				});
				let files = res.tempFilePaths.map((item,index) => {
					return {
						name:'files',
						file:res.tempFiles[index],
						uri:item
					}
				})
				uni.uploadFile({
					url: apiUrl,
					files: files,
					name: 'files',
					success: (opt) => {
						let data = JSON.parse(opt.data);
						console.log("图片返回", data);
						uni.hideLoading();

						if (data.code == 200) {
							relove(data.data);
						} else {
							reject();
						}

					},
					fail: (err) => {
						uni.hideLoading();
						reject();
					}
				});

			}
		});

	})

}